/**
 * 
 */
package com.jdy.haoduoaiteacher.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import com.jdy.haoduoaiteacher.util.Tools;
import com.ycsj.goldmedalnewconcept.R;

/**
 * @author zp 2016-6-23 ����11:40:33
 */

public class BatteryIndicator extends View {

  protected int WIDTH;

  protected int HEIGHT;

  private final int indicatorColor = Color.argb(255, 255, 255, 255);

  private final Paint mPaint;

  private final Bitmap mFullBatteryBmp;

  private final Bitmap mBatteryChargingBmp;

  private int BITMAP_W = 0;

  private int BITMAP_H = 0;

  private float pointer = 0;

  private final Handler mHandler = new Handler();

  private Runnable updatePointerRunnable;

  private boolean forward = true;

  private float VERTICAL_GAP = 18;

  private float HORIZONTAL_GAP = 20;

  private float BATTERY_TIP_WIDTH = 5;

  private final float FACTOR = 0.6f;// 0.6 equals to 1.0.

  private float ratio = 1;

  private boolean charging = false;

  /**
   * @param context
   */
  public BatteryIndicator(Context context) {
    this(context, null);
    // TODO Auto-generated constructor stub
  }

  /**
   * @param context
   * @param attrs
   */
  public BatteryIndicator(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
    // TODO Auto-generated constructor stub
  }

  /**
   * @param context
   * @param attrs
   * @param defStyleAttr
   */
  public BatteryIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    // TODO Auto-generated constructor stub

    ratio = Tools.getPixelRatio(context);

    VERTICAL_GAP *= ratio;

    HORIZONTAL_GAP *= ratio;

    BATTERY_TIP_WIDTH *= ratio;

    mPaint = new Paint();

    mFullBatteryBmp = BitmapFactory.decodeResource(getResources(),
        R.drawable.battery_full);

    mBatteryChargingBmp = BitmapFactory.decodeResource(getResources(),
        R.drawable.battery_charging);

    mPaint.setAntiAlias(true);

    mPaint.setStyle(Style.FILL_AND_STROKE);

    mPaint.setColor(indicatorColor);

    if (mFullBatteryBmp != null) {
      BITMAP_W = mFullBatteryBmp.getWidth();

      pointer = BITMAP_W;

      BITMAP_H = mFullBatteryBmp.getHeight();
    }

    updatePointerRunnable = new Runnable() {
      @Override
      public void run() {
        try {
          if (forward) {
            pointer++;
            if (pointer > 0 + BITMAP_W) {
              forward = false;
            }
          } else {
            pointer--;
            if (pointer < 0) {
              forward = true;
            }
          }

          invalidate();
        } finally {
          // 100% guarantee that this always happens, even if
          // your update method throws an exception
          mHandler.postDelayed(updatePointerRunnable, 30);
        }
      }
    };

    // updatePointerRunnable.run();

    if (isInEditMode())
      return;

  }

  @Override
  protected void onSizeChanged(int w, int h, int oldW, int oldH) {
    WIDTH = w;

    HEIGHT = h;

  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int desiredWidth = 70;

    int desiredHeight = 60;

    desiredWidth *= ratio;

    desiredHeight *= ratio;

    int widthMode = MeasureSpec.getMode(widthMeasureSpec);

    int widthSize = MeasureSpec.getSize(widthMeasureSpec);

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    int width;

    int height;

    // Measure Width
    if (widthMode == MeasureSpec.EXACTLY) {
      // Must be this size
      width = widthSize;
    } else if (widthMode == MeasureSpec.AT_MOST) {
      // Can't be bigger than...
      width = Math.min(desiredWidth, widthSize);
    } else {
      // Be whatever you want
      width = desiredWidth;
    }

    // Measure Height
    if (heightMode == MeasureSpec.EXACTLY) {
      // Must be this size
      height = heightSize;
    } else if (heightMode == MeasureSpec.AT_MOST) {
      // Can't be bigger than...
      height = Math.min(desiredHeight, heightSize);
    } else {
      // Be whatever you want
      height = desiredHeight;
    }

    // MUST CALL THIS
    setMeasuredDimension(width, height);
  }

  /**
   * this should be in the range of 0..1
   * 
   * @param batteryPercent
   */
  public void updateBattery(float batteryPercent) {
    if (batteryPercent > 1)
      batteryPercent = 1.0f;
    else if (batteryPercent < 0)
      batteryPercent = 0;
    final float p = batteryPercent * FACTOR;
    mHandler.postDelayed(new Runnable() {

      @Override
      public void run() {
        // TODO Auto-generated method stub
        pointer = BITMAP_W * p;
        invalidate();
      }
    }, 100);
  }

  public void setChargeState(boolean enabled) {
    charging = enabled;
    mHandler.postDelayed(new Runnable() {

      @Override
      public void run() {
        // TODO Auto-generated method stub
        invalidate();
      }
    }, 100);
  }

  @Override
  protected void onDraw(Canvas canvas) {

    // canvas.drawRect(0, 0, WIDTH, HEIGHT, null);
    if (!charging) {
      if (mFullBatteryBmp != null)
        canvas.drawBitmap(mFullBatteryBmp, 0, 0, null);
      canvas.drawRect(pointer + HORIZONTAL_GAP / 2, VERTICAL_GAP / 2, BITMAP_W
          - HORIZONTAL_GAP / 2 - BATTERY_TIP_WIDTH,
          BITMAP_H - VERTICAL_GAP / 2, mPaint);
    } else {
      if (mBatteryChargingBmp != null)
        canvas.drawBitmap(mBatteryChargingBmp, 0, 0, null);
    }
  }

}
